package Utils;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * <b>This class used to create the log files to your project.<br>To easily
 * trace the Errors.</b><br><br>The best way to call it is
 * MSJ_Log.Log("");<br><br>Create file called Log beside the .jar file.
 *
 * @author mohamed Tarek Salama
 * @version 1.2 BETA
 */
public class MSJ_Log {

    private final static Logger log = Logger.getLogger("MyLog");
    private static FileHandler filehandler;

    /**
     * Write the Strings to the file.
     *
     * @param Message : String
     * @exception IOException : Show MSJ_JOptionPane.
     * @exception SecurityException : Show MSJ_JOptionPane.
     */
    public static void Log(String Message) {
        if (filehandler == null) {
            try {
                filehandler = new FileHandler(MSJ_SysProperty.getUser_dir() + "//Log.log", true);
                log.addHandler(filehandler);
                log.setLevel(Level.ALL);
                SimpleFormatter formatter = new SimpleFormatter();
                filehandler.setFormatter(formatter);
            } catch (IOException ex) {
                MSJ_JOptionPane.show_Error_Message("Error make the file\nOr Error write on it.\n" + ex.toString());
            } catch (SecurityException ex) {
                MSJ_JOptionPane.show_Error_Message("Found Security that prevent\n create the file.\n" + ex.toString());
            }
        }
        log.log(Level.ALL, Message);
        filehandler.flush();
    }
}